package com.hanscan.gui;

import com.hanscan.persistence.controllers.CompanyJpaController;
import com.hanscan.persistence.controllers.TerminalJpaController;
import com.hanscan.persistence.controllers.exceptions.NonexistentEntityException;
import com.hanscan.persistence.entities.Company;
import com.hanscan.persistence.entities.Terminal;
import com.hanscan.util.language.LanguageConfig;
import com.icesoft.faces.component.ext.HtmlCommandButton;
import com.icesoft.faces.component.ext.HtmlInputText;
import com.icesoft.faces.component.ext.HtmlOutputLabel;
import com.icesoft.faces.component.ext.HtmlPanelGrid;
import com.icesoft.faces.component.ext.RowSelectorEvent;
import com.icesoft.faces.component.panelstack.PanelStack;
import com.sun.rave.web.ui.appbase.AbstractFragmentBean;
import java.util.ArrayList;
import java.util.List;
import javax.faces.FacesException;
import javax.faces.event.ActionEvent;


public class ManagementTerminal extends AbstractFragmentBean
{
    private PanelStack panelStack = new PanelStack();
    private PanelStack panelStackMainMenu = new PanelStack();

    private HtmlOutputLabel labelMenssage = new HtmlOutputLabel();
    private HtmlOutputLabel labelName = new HtmlOutputLabel();
    private HtmlOutputLabel labelFilterByName = new HtmlOutputLabel();
    private HtmlOutputLabel labelNameBaja = new HtmlOutputLabel();
    private HtmlOutputLabel labelTextNameBaja = new HtmlOutputLabel();
    private HtmlOutputLabel labelNameEdit = new HtmlOutputLabel();
    private HtmlOutputLabel labelMac = new HtmlOutputLabel();
    private HtmlOutputLabel labelMacBaja = new HtmlOutputLabel();
    private HtmlOutputLabel labelMacEditar = new HtmlOutputLabel();
    private HtmlOutputLabel labelTextMacBaja = new HtmlOutputLabel();
    private HtmlOutputLabel labelFilterByMac = new HtmlOutputLabel();

    private int estado = 0;

    private HtmlInputText inputTextFilterByName = new HtmlInputText();
    private HtmlInputText inputTextName = new HtmlInputText();   
    private HtmlInputText inputTextNameEdit = new HtmlInputText();
    private HtmlInputText inputTextMac = new HtmlInputText();   
    private HtmlInputText inputTextMacEdit = new HtmlInputText();
    private HtmlInputText inputTextFilterByMAc = new HtmlInputText();

    private HtmlCommandButton btnSaveCompany = new HtmlCommandButton();
    private HtmlCommandButton btnSearchUser = new HtmlCommandButton();
    private HtmlCommandButton btnAdd = new HtmlCommandButton();
    private HtmlCommandButton btnEdit = new HtmlCommandButton();
    private HtmlCommandButton btnRemove = new HtmlCommandButton();
    private HtmlCommandButton btnRemoveCompany = new HtmlCommandButton();
    private HtmlCommandButton btnEditCompany = new HtmlCommandButton();

    private HtmlPanelGrid containerSearch = new HtmlPanelGrid();
    private HtmlPanelGrid containerFilter = new HtmlPanelGrid();

    private Company[] companies;
    private boolean ascending;
    private Company company = new Company();

    private String sortColumnName;
    private String clientNameHeader;

    private String colorEmptyField = "background-color:rgba(255, 0, 0, 0.5);";


    private void _init() throws Exception
    {
        loadLanguage(getSessionBean1().getSessionBeanLanguage());
    }

    public ManagementTerminal()
    {
    }

    protected void loadLanguage(LanguageConfig languageConfig)
    {
        btnAdd.setValue(languageConfig.getProperty("Nuevo"));
        btnEdit.setValue(languageConfig.getProperty("Modificar"));
        btnRemove.setValue(languageConfig.getProperty("Suprimir"));
        labelName.setValue(languageConfig.getProperty("Nombre") + " " + "*");
        btnSearchUser.setValue(languageConfig.getProperty("Buscar"));
        labelFilterByName.setValue(languageConfig.getProperty("Nombre"));
        btnSaveCompany.setValue(languageConfig.getProperty("Guardar"));
        clientNameHeader = languageConfig.getProperty("Nombre");
        labelNameBaja.setValue(languageConfig.getProperty("Nombre"));
        labelNameEdit.setValue(languageConfig.getProperty("Nombre"));
        btnEditCompany.setValue(languageConfig.getProperty("Editar"));
        btnRemoveCompany.setValue(languageConfig.getProperty("Borrar"));
        labelMenssage.setValue(languageConfig.getProperty("Terminales"));
        labelFilterByMac.setValue(languageConfig.getProperty("Mac"));
        labelMac.setValue(languageConfig.getProperty("Mac"));
        labelMacEditar.setValue(languageConfig.getProperty("Mac"));
        labelMacBaja.setValue(languageConfig.getProperty("Mac"));
    }

    public void rowSelectionListener(RowSelectorEvent event)
    {
        company = companies[event.getRow()];
        if(estado == 1)
        {
            labelTextNameBaja.setValue(company.getCompanyName());
            panelStackMainMenu.setSelectedPanel("panelCompanieData");
            panelStack.setSelectedPanel("panelRemoveCompany");
        }

        if(estado == 2)
        {
            inputTextNameEdit.setValue(company.getCompanyName());
            panelStackMainMenu.setSelectedPanel("panelCompanieData");
            panelStack.setSelectedPanel("panelEditCompany");
        }
        containerSearch.setRendered(false);
        containerFilter.setRendered(false);
    }

    public String btnAdd_action(ActionEvent e)
    {
        estado = 0;
        labelMenssage.setValue(getSessionBean1().getSessionBeanLanguage().getProperty("Alta_Terminal"));
        panelStackMainMenu.setSelectedPanel("panelCompanieData");
        panelStack.setSelectedPanel("panelAddCompany");
        return null;
    }

    public void initColorInputText(HtmlInputText inputText)
    {
        inputText.setStyle("");
    }

    public void changeColorInputText(HtmlInputText inputText)
    {
        if (inputText.getText().equals(""))
        {
            inputText.setStyle(colorEmptyField);
        }
        else
        {
            inputText.setStyle("");
        }
    }

    public String btnRemove_action(ActionEvent e)
    {
        estado = 1;
        labelMenssage.setValue(getSessionBean1().getSessionBeanLanguage().getProperty("Borrar_Terminal"));
        containerFilter.setRendered(true);
        panelStackMainMenu.setSelectedPanel("panelFilter");
        return null;
    }

    public String btnEdit_action(ActionEvent e)
    {
        estado = 2;
        labelMenssage.setValue(getSessionBean1().getSessionBeanLanguage().getProperty("Editar_Terminal"));
        containerFilter.setRendered(true);
        panelStackMainMenu.setSelectedPanel("panelFilter");
        containerSearch.setRendered(false);
        return null;
    }

    public String btnRemoveCompany_action(ActionEvent e)
    {
        try
        {
            CompanyJpaController companyJpaController = new CompanyJpaController();
            companyJpaController.destroy(company);
            containerSearch.setRendered(false);
//     TODO       panelPopupPruebaBean.setShowModalPanel(true);
//            messageHeader = "Informacion";
//            messageText = "Operación realizada correctamente";
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
//     TODO       panelPopupPruebaBean.setShowModalPanel(true);
//            messageHeader = "Error";
//            messageText = "Error al borrar de base de datos";
        }
        return null;
    }

    public String btnEditCompany_action(ActionEvent e)
    {
        try
        {
            CompanyJpaController companyJpaController = new CompanyJpaController();
            company.setCompanyName(inputTextNameEdit.getValue().toString());
            companyJpaController.edit(company);
//     TODO       panelPopupPruebaBean.setShowModalPanel(true);
//            messageHeader = "Informacion";
//            messageText = "Operación realizada correctamente";
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
//     TODO       panelPopupPruebaBean.setShowModalPanel(true);
//            messageHeader = "Error";
//            messageText = "Error al borrar de base de datos";
        }
        return null;
    }


    public String btnSaveCompany_action(ActionEvent e)
    {
        CompanyJpaController companyJpaController = new CompanyJpaController();
        Company auxCompany = new Company();
        auxCompany.setCompanyName(inputTextName.getValue().toString());
        companyJpaController.create(auxCompany);
        return null;
    }

    public String btnSearchUser_action(ActionEvent e)
    {
        String name = inputTextFilterByName.getValue().toString();
        String mac = inputTextFilterByMAc.getValue().toString();

        List<Terminal> listTerminal = new ArrayList();

        if(name.equals(""))
        {
            if(mac.equals(""))
            {
                try
                {
                    TerminalJpaController terminalJpaController = new TerminalJpaController();
                    listTerminal = terminalJpaController.getAllCompaniesOrderByName();
                    companies = new Company[listCompany.size()];
                    for(int i = 0; i < listCompany.size(); i++)
                    {
                       companies[i] =  listCompany.get(i);
                    }

                }
                catch (Exception ex)
                {
                   ex.printStackTrace();
    //      TODO         panelPopupPruebaBean.setShowModalPanel(true);
    //               messageHeader = "Error";
    //               messageText = "Error en la busqueda de base de datos";
                }
            }
        }
        else
        {
            try
            {
                CompanyJpaController companyJpaController = new CompanyJpaController();
                listCompany = companyJpaController.findCompanyByName(busqueda);
                companies = new Company[listCompany.size()];
                for(int i = 0; i < listCompany.size(); i++)
                {
                   companies[i] =  listCompany.get(i);
                }
            }
            catch (NonexistentEntityException ex)
            {
                ex.printStackTrace();
//   TODO             panelPopupPruebaBean.setShowModalPanel(true);
//                messageHeader = "Error";
//                messageText = "Error en al busqueda de base de datos";
            }

        }
        containerSearch.setRendered(true);
        return null;
    }


    @Override
    public void init()
    {
        super.init();
        try
        {
            _init();
        }
        catch (Exception e)
        {
            log("Page1 Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
        }
    }

    @Override
    public void destroy()
    {
    }

    //////////////////////////////////////////////////////////
    /*GETTER y SETTER*/
    //////////////////////////////////////////////////////////

    protected RequestBean1 getRequestBean1()
    {
        return (RequestBean1) getBean("RequestBean1");
    }

    protected SessionBean1 getSessionBean1()
    {
        return (SessionBean1) getBean("SessionBean1");
    }

    protected ApplicationBean1 getApplicationBean1()
    {
        return (ApplicationBean1) getBean("ApplicationBean1");
    }

    public HtmlInputText getInputTextName() {
        return inputTextName;
    }

    public void setInputTextName(HtmlInputText inputTextName) {
        this.inputTextName = inputTextName;
    }

    public HtmlOutputLabel getLabelMenssage() {
        return labelMenssage;
    }

    public void setLabelMenssage(HtmlOutputLabel labelMenssage) {
        this.labelMenssage = labelMenssage;
    }

    public HtmlOutputLabel getLabelName() {
        return labelName;
    }

    public void setLabelName(HtmlOutputLabel labelName) {
        this.labelName = labelName;
    }

    public PanelStack getPanelStack() {
        return panelStack;
    }

    public void setPanelStack(PanelStack panelStack) {
        this.panelStack = panelStack;
    }

    public HtmlCommandButton getBtnSaveCompany() {
        return btnSaveCompany;
    }

    public void setBtnSaveCompany(HtmlCommandButton btnSaveCompany) {
        this.btnSaveCompany = btnSaveCompany;
    }

    public HtmlCommandButton getBtnAdd() {
        return btnAdd;
    }

    public void setBtnAdd(HtmlCommandButton btnAdd) {
        this.btnAdd = btnAdd;
    }

    public HtmlCommandButton getBtnEdit() {
        return btnEdit;
    }

    public void setBtnEdit(HtmlCommandButton btnEdit) {
        this.btnEdit = btnEdit;
    }

    public HtmlCommandButton getBtnRemove() {
        return btnRemove;
    }

    public void setBtnRemove(HtmlCommandButton btnRemove) {
        this.btnRemove = btnRemove;
    }

    public HtmlCommandButton getBtnSearchUser() {
        return btnSearchUser;
    }

    public void setBtnSearchUser(HtmlCommandButton btnSearchUser) {
        this.btnSearchUser = btnSearchUser;
    }

    public HtmlInputText getInputTextFilterByName() {
        return inputTextFilterByName;
    }

    public void setInputTextFilterByName(HtmlInputText inputTextFilterByName) {
        this.inputTextFilterByName = inputTextFilterByName;
    }

    public HtmlOutputLabel getLabelFilterByName() {
        return labelFilterByName;
    }

    public void setLabelFilterByName(HtmlOutputLabel labelFilterByName) {
        this.labelFilterByName = labelFilterByName;
    }

    public HtmlPanelGrid getContainerFilter() {
        return containerFilter;
    }

    public void setContainerFilter(HtmlPanelGrid containerFilter) {
        this.containerFilter = containerFilter;
    }

    public HtmlPanelGrid getContainerSearch() {
        return containerSearch;
    }

    public void setContainerSearch(HtmlPanelGrid containerSearch) {
        this.containerSearch = containerSearch;
    }

    public boolean isAscending() {
        return ascending;
    }

    public void setAscending(boolean ascending) {
        this.ascending = ascending;
    }

    public Company[] getCompanies() {
        return companies;
    }

    public void setCompanies(Company[] companies) {
        this.companies = companies;
    }

    public String getSortColumnName() {
        return sortColumnName;
    }

    public void setSortColumnName(String sortColumnName) {
        this.sortColumnName = sortColumnName;
    }

    public String getClientNameHeader() {
        return clientNameHeader;
    }

    public void setClientNameHeader(String clientNameHeader) {
        this.clientNameHeader = clientNameHeader;
    }

    public HtmlCommandButton getBtnEditCompany() {
        return btnEditCompany;
    }

    public void setBtnEditCompany(HtmlCommandButton btnEditCompany) {
        this.btnEditCompany = btnEditCompany;
    }

    public HtmlCommandButton getBtnRemoveCompany() {
        return btnRemoveCompany;
    }

    public void setBtnRemoveCompany(HtmlCommandButton btnRemoveCompany) {
        this.btnRemoveCompany = btnRemoveCompany;
    }

    public HtmlOutputLabel getLabelNameBaja() {
        return labelNameBaja;
    }

    public void setLabelNameBaja(HtmlOutputLabel labelNameBaja) {
        this.labelNameBaja = labelNameBaja;
    }

    public HtmlOutputLabel getLabelTextNameBaja() {
        return labelTextNameBaja;
    }

    public void setLabelTextNameBaja(HtmlOutputLabel labelTextNameBaja) {
        this.labelTextNameBaja = labelTextNameBaja;
    }

    public HtmlInputText getInputTextNameEdit() {
        return inputTextNameEdit;
    }

    public void setInputTextNameEdit(HtmlInputText inputTextNameEdit) {
        this.inputTextNameEdit = inputTextNameEdit;
    }

    public HtmlOutputLabel getLabelNameEdit() {
        return labelNameEdit;
    }

    public void setLabelNameEdit(HtmlOutputLabel labelNameEdit) {
        this.labelNameEdit = labelNameEdit;
    }

    public PanelStack getPanelStackMainMenu() {
        return panelStackMainMenu;
    }

    public void setPanelStackMainMenu(PanelStack panelStackMainMenu) {
        this.panelStackMainMenu = panelStackMainMenu;
    }

    public Company getCompany() {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }

    public int getEstado() {
        return estado;
    }

    public void setEstado(int estado) {
        this.estado = estado;
    }

    public HtmlInputText getInputTextMac() {
        return inputTextMac;
    }

    public void setInputTextMac(HtmlInputText inputTextMac) {
        this.inputTextMac = inputTextMac;
    }

    public HtmlInputText getInputTextMacEdit() {
        return inputTextMacEdit;
    }

    public void setInputTextMacEdit(HtmlInputText inputTextMacEdit) {
        this.inputTextMacEdit = inputTextMacEdit;
    }

    public HtmlOutputLabel getLabelMac() {
        return labelMac;
    }

    public void setLabelMac(HtmlOutputLabel labelMac) {
        this.labelMac = labelMac;
    }

    public HtmlOutputLabel getLabelMacBaja() {
        return labelMacBaja;
    }

    public void setLabelMacBaja(HtmlOutputLabel labelMacBaja) {
        this.labelMacBaja = labelMacBaja;
    }

    public HtmlOutputLabel getLabelMacEditar() {
        return labelMacEditar;
    }

    public void setLabelMacEditar(HtmlOutputLabel labelMacEditar) {
        this.labelMacEditar = labelMacEditar;
    }

    public HtmlOutputLabel getLabelTextMacBaja() {
        return labelTextMacBaja;
    }

    public void setLabelTextMacBaja(HtmlOutputLabel labelTextMacBaja) {
        this.labelTextMacBaja = labelTextMacBaja;
    }

    public HtmlInputText getInputTextFilterByMAc() {
        return inputTextFilterByMAc;
    }

    public void setInputTextFilterByMAc(HtmlInputText inputTextFilterByMAc) {
        this.inputTextFilterByMAc = inputTextFilterByMAc;
    }

    public HtmlOutputLabel getLabelFilterByMac() {
        return labelFilterByMac;
    }

    public void setLabelFilterByMac(HtmlOutputLabel labelFilterByMac) {
        this.labelFilterByMac = labelFilterByMac;
    }

}
